﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Extensions.Caching.Memory;
using MonkeyCache;
using MonkeyCache.LiteDB;
using PersistentMemoryCache;
using TestModels;
using WpfApp1.Models;

namespace WpfApp1.Views;

public partial class MonkeyCacheWindow1 : Window
{
    public MonkeyCacheWindow1()
    {
        InitializeComponent();
        string folder = @"C:\Users\sam\AppData\Roaming\PxSemicDMSClient";
        BarrelUtils.SetBaseCachePath(folder);
        Barrel.ApplicationId = "MonkeyCacheWindow1";
        Barrel.Current.AutoExpire = true;

        bcache = Barrel.Current;

        //IMemoryCache cache = new PersistentMemoryCache(new PersistentMemoryCacheOptions("Test", new LiteDbStore(new LiteDbOptions("Test.db"))));
        ldbcache = new PersistentCache(new PersistentCacheOptions("litedbtest", new LiteDbStore(new LiteDbOptions("testlitedbcache.db"))) { });
    }

    private readonly IBarrel bcache;
    private IMemoryCache ldbcache;

    TimeSpan expirationSpan = TimeSpan.FromSeconds(6);  // 6秒后过期

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        MemoryCacheEntryOptions options = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(6) };
        var cat = new Cat { Id = 1, CatColor = 2, Version = new Version(2, 3, 4, 5) };
        var dog = new Dog { Id = 2, DogType = 2, Version = new Version(3, 4, 5, 6) };
        var node1 = new NodeEntity { Id = 2, Name = "node first", Animal = cat };
        var node2 = new NodeEntity { Id = 3, Name = "node second", Animal = dog };
        //bcache.Add<Animal>("cat1", cat, TimeSpan.FromSeconds(10));

        ldbcache.Set("node1", node1, options);
        ldbcache.Set("node2", node2, options);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //var c1 = bcache.Get<Animal>("cat1");
        var c1 = ldbcache.Get<NodeEntity>("node1");
        var c2 = ldbcache.Get<NodeEntity>("node2");
        var a = c1;
    }
}